package com.privat.einkaufsscan;

import java.util.ArrayList;
import java.util.List;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;

public class DAO {
	private SQLiteDatabase database;
	private SQLHelper dbHelper;
	private String[] allColumns = { SQLHelper.COLUMN_ID ,
									SQLHelper.COLUMN_CODE,
									SQLHelper.COLUMN_PRODUKT,
									SQLHelper.COLUMN_ORT,
									SQLHelper.COLUMN_COUNT};

	public DAO(Context context) {
		dbHelper = new SQLHelper(context);
	}

	private void open() throws SQLException {
		database = dbHelper.getWritableDatabase();
	}

	private void close() {
		dbHelper.close();
	}
	
	public Entry createEntry(String code, String produkt, String ort, int count)
	{
		open();
		ContentValues values = new ContentValues();
		values.put(SQLHelper.COLUMN_CODE, code);
		values.put(SQLHelper.COLUMN_PRODUKT, produkt);
		values.put(SQLHelper.COLUMN_ORT, ort);
		values.put(SQLHelper.COLUMN_COUNT, count);
		
		long insertId = database.insert(SQLHelper.TABLE_NAME, null, values);
		
		Cursor cursor = database.query(SQLHelper.TABLE_NAME,
				allColumns, SQLHelper.COLUMN_ID + " = " + insertId, null,
				null, null, null);
		
		cursor.moveToFirst();
		Entry newEntry = cursorToEntry(cursor);
		cursor.close();
		close();
		return newEntry;
	}
	
	public void deleteEntry(Entry entry)
	{
		open();
		database.delete(SQLHelper.TABLE_NAME, SQLHelper.COLUMN_ID + " = " + entry.getId(), null);
		close();
	}
	
	public void updateEntry(String code, int count)
	{
		ContentValues values = new ContentValues();
	    values.put(SQLHelper.COLUMN_COUNT, count);
	    open();
		database.update(SQLHelper.TABLE_NAME, values, SQLHelper.COLUMN_CODE + "=?", new String[]{code});
		close();
	}
	
	public List<Entry> getAllEntrys() {
		open();
		List<Entry> entrys = new ArrayList<Entry>();

		Cursor cursor = database.query(SQLHelper.TABLE_NAME,
				allColumns, null, null, null, null, null);

		cursor.moveToFirst();
		while (!cursor.isAfterLast()) {
			Entry entry = cursorToEntry(cursor);
			entrys.add(entry);
			cursor.moveToNext();
		}
		// Make sure to close the cursor
		cursor.close();
		close();
		return entrys;
	}
	
	public List<Entry> getAllExisting() {
		open();
		List<Entry> entrys = new ArrayList<Entry>();

		Cursor cursor = database.query(SQLHelper.TABLE_NAME, allColumns, SQLHelper.COLUMN_COUNT + ">?", new String[]{"0"}, null ,null, null );

		cursor.moveToFirst();
		while (!cursor.isAfterLast()) {
			Entry entry = cursorToEntry(cursor);
			entrys.add(entry);
			cursor.moveToNext();
		}
		// Make sure to close the cursor
		cursor.close();
		close();
		return entrys;
	}
	
	public Entry getEntry(String input)
	{
		SQLiteDatabase db=dbHelper.getReadableDatabase();

		Cursor cursor = db.query(SQLHelper.TABLE_NAME, allColumns, SQLHelper.COLUMN_CODE + "=?", new String[]{input}, null, null, null);

		if (cursor != null)
		{
			cursor.moveToFirst();
			if(!cursor.isAfterLast())
			{
				Entry entry = cursorToEntry(cursor);
				cursor.close();
				db.close();
				return entry;
			}
			else
			{
				cursor.close();
				db.close();
				return null;
			}
		}
		else
		{
			db.close();
			return null;
		}	
	}
	
	private Entry cursorToEntry(Cursor cursor) {
		Entry tmpentry = new Entry();
		tmpentry.setId(cursor.getInt(0));
		tmpentry.setCode(cursor.getString(1));
		tmpentry.setProdukt(cursor.getString(2));
		tmpentry.setOrt(cursor.getString(3));
		tmpentry.setCount(cursor.getInt(4));
		return tmpentry;
	}

}
